unit NewProjectDlg;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ActnList, StdActns, XPStyleActnCtrls,
  ActnMan, Grids, Index;

type
  TfrmNewProject = class(TForm)
    lblFolder: TLabel;
    ebLocation: TEdit;
    Label1: TLabel;
    ebProjectName: TEdit;
    btnLocation: TButton;
    btnOK: TButton;
    btnCancel: TButton;
    ActionManager1: TActionManager;
    ProjectLocation: TBrowseForFolder;
    lblFirstYear: TLabel;
    ebFirstYear: TEdit;
    Label2: TLabel;
    ebLastYear: TEdit;
    Label3: TLabel;
    ebFirstAge: TEdit;
    ebLastAge: TEdit;
    Label4: TLabel;
    sgData: TStringGrid;
    btnSelectFile: TButton;
    rgDataset: TRadioGroup;
    OpenDlg: TOpenDialog;
    procedure ProjectLocationBeforeExecute(Sender: TObject);
    procedure ProjectLocationAccept(Sender: TObject);
    procedure sgDataSelectCell(Sender: TObject; ACol, ARow: Integer;
      var CanSelect: Boolean);
    procedure FormCreate(Sender: TObject);
    procedure rgDatasetClick(Sender: TObject);
    procedure btnSelectFileClick(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  private
      procedure UpdateSelectFileBtn(ARow : Integer);
  public
    { Public declarations }
  end;

var
  frmNewProject: TfrmNewProject;

function NumIndicesFromFile(FileName : string; Fleet : Boolean) : Integer;


implementation

{$R *.dfm}


function NumIndicesFromFile(FileName : string; Fleet : Boolean) : Integer;
var
   NumIndInFile, NumYearsInFile, VPALoc : Integer;
   F : TextFile;
begin
   Result := 0;
   if not FileExists(FileName) then exit;
   AssignFile(F, FileName);
   Reset(F);
   readln(F);
   if Fleet then
   begin
      readln(F, NumIndInFile);
      Result := NumIndInFile - 100;
   end
   else
   begin
      readln(F, NumIndInFile, NumYearsInFile, VPALoc);
      Result := NumIndInFile;
   end;
   CloseFile(F);
end; // NumIndicesFromFile


procedure TfrmNewProject.btnSelectFileClick(Sender: TObject);
begin
   if OpenDlg.Execute() then
   begin
      sgData.Cells[1, sgData.Row] := OpenDlg.FileName;
   end;
end;

procedure TfrmNewProject.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
begin
   if ModalResult = mrOK then
   begin
      CanClose := false;
      if not DirectoryExists(ebLocation.Text) then
      begin
         MessageDlg('Please choose a valid location!', mtError, [mbok], 0);
         ebLocation.SetFocus;
         exit;
      end;
      if DirectoryExists(ebLocation.Text + '\' + ebProjectName.Text) then
      begin
         MessageDlg('Project name allready exists. Please choose another name!', mtError, [mbok], 0);
         ebProjectName.SetFocus;
         exit;
      end;
   end;
   CanClose := true;
end;

procedure TfrmNewProject.FormCreate(Sender: TObject);
var
   numbase : string;
   I : Integer;
begin
   sgData.ColWidths[0] := 80;
   sgData.ColWidths[1] := sgData.Width - 84;
   sgData.Cells[0, 0] := 'Datasource';
   sgData.Cells[1, 0] := 'Location';
   for I := 0 to length(c_DataSourceEditCategories) - 1 do
      sgData.Cells[0, I + 1] := c_DataSourcesetup[c_DataSourceEditCategories[I]].Key;
   rgDatasetClick(nil);
end; // TfrmNewProject.FormCreate


procedure TfrmNewProject.ProjectLocationAccept(Sender: TObject);
begin
   ebLocation.Text := ProjectLocation.Folder;
end; // TfrmNewProject.ProjectLocationAccept


procedure TfrmNewProject.ProjectLocationBeforeExecute(Sender: TObject);
begin
   ProjectLocation.Folder := ebLocation.Text;
end;


procedure TfrmNewProject.UpdateSelectFileBtn(ARow : Integer);
begin
   btnSelectFile.Top := 168 + ARow * 19;
end; // TfrmNewProject.UpdateSelectFileBtn


procedure TfrmNewProject.rgDatasetClick(Sender: TObject);
begin
   btnSelectFile.Enabled := rgDataset.ItemIndex = 1;
   sgData.Enabled := btnSelectFile.Enabled;
   if sgData.Enabled then
      sgData.Color := clWindow
   else
      sgData.Color := clBtnFace;
   UpdateSelectFileBtn(sgData.Row);
end;

procedure TfrmNewProject.sgDataSelectCell(Sender: TObject; ACol,
  ARow: Integer; var CanSelect: Boolean);
begin
   UpdateSelectFileBtn(ARow);
end;

// TfrmNewProject.ProjectLocationBeforeExecute

end.
